package org.jboss.edt;

import java.util.Iterator;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.richfaces.component.html.HtmlExtendedDataTable;
import org.richfaces.model.selection.SimpleSelection;

@Name("EDTAction")
@Scope(ScopeType.PAGE)
public class EDTAction {

	@In(required=false)
	@Out(required=false)
	private HtmlExtendedDataTable personsTable;

	private SimpleSelection personSelection;

	@In(value = "PersonDataModel", create = true)
	private PersonDataModel personsDataModel;

	private Person selectedPerson;

	public void selectPerson() {
		if (personsTable == null || personSelection == null) {
			selectedPerson = null;
		}
		// backup
		Object rowKey = personsTable.getRowKey();
		Iterator<Object> iterator = personSelection.getKeys();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			personsTable.setRowKey(key);
			if (personsTable.isRowAvailable()) {
				selectedPerson = (Person) personsTable.getRowData();
				// restore backup
				personsTable.setRowKey(rowKey);
			}
		}
		personsTable.setRowKey(rowKey);
	}

	public HtmlExtendedDataTable getPersonsTable() {
		return personsTable;
	}

	public SimpleSelection getPersonSelection() {
		return personSelection;
	}

	public void setPersonSelection(SimpleSelection personSelection) {
		this.personSelection = personSelection;
	}

	public PersonDataModel getPersonsDataModel() {
		return personsDataModel;
	}

	public Person getSelectedPerson() {
		return selectedPerson;
	}
}
